import { getLoadMapList, getTenantID, requestFunComponFiex, commonManageSearchParams, getCompanyID } from '@/utils';
import { useRequest } from 'umi';
import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { adminAreadDataGet } from '@/services/BaseLayout';
import { useCallback, useMemo, useState } from 'react';
import { OutInRecordManageAPI } from '../common/type';
import { ManageSearchColumns, RecordManageTab } from '../mconfig';

import { getBaseUrl } from '@/services/urlconst';

let baseUrl = getBaseUrl();
// let baseUrl = '/wenchuang'; // 本地环境  todo

const ManageServices = (curRes: Record<string, any>) => {
  /***获取数据字典**/
  const loadMdicIds: string[] = [`${getTenantID()}${ILoadMapTypeAll.STORAGE_INSPECTION_STATUS}`, `${getTenantID()}${ILoadMapTypeAll.ASSET_APPROVE_STATUS}`];

  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取省市区数据 */
  const areaList = useRequest(() => {
    return adminAreadDataGet({});
  });

  /**行政区域数据 */
  let areaListData = useMemo(() => {
    if (areaList?.data?.msgFlag) {
      return areaList?.data?.obj;
    } else {
      return [];
    }
  }, [areaList?.data]);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = RecordManageTab(curRes);

  /** 获取搜索参数 */
  const searchListParams = ManageSearchColumns();

  /***保存当前的搜索参数**/
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /***管理页获取数据方法**/
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: OutInRecordManageAPI.IAssetTableRequestType) => {
      let PostParams = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        createCompanyID: getCompanyID(),
        dicTypeIds: loadMdicIds?.toString(), // 字典入参
        ...reqParams,
      };

      const reqbody = PostParams;
      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, reqbody, {
        // 需要翻译的字典属性名
        dicTransName: 'applyState,checkState,',
        // 需要翻译的机构属性名
        orgTransName: `${ISearchTranslateAll.SIGN_DEPT},${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.OWNER_DEPT},${ISearchTranslateAll.CANCEL_DEPT},${ISearchTranslateAll.DUTY_DEPT},${ISearchTranslateAll.HAND_DEPT}`,
        // 需要翻译的区域属性名
        // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      });
      setManageTableParams(searchParams);
      const reqUrl = `${baseUrl}/assetStoreRecord/dataGrid/${queryTypeObj?.params}`;
      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, reqbody);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /***重新请求的方法**/
  const reloadRequest = useCallback((reqParams: OutInRecordManageAPI.IAssetTableRequestType | any) => {
    /**处理创建时间*/
    if (reqParams?.createTime) {
      const [timeStart, timeEnd] = reqParams.createTime;
      reqParams['createTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['createTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['createTime'];
    }

    /**处理申请时间*/
    if (reqParams?.ownTime) {
      const [timeStart, timeEnd] = reqParams.ownTime;
      reqParams['ownTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['ownTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['ownTime'];
    }

    /**处理签发时间*/
    if (reqParams?.signTime) {
      const [timeStart, timeEnd] = reqParams.signTime;
      reqParams['signTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['signTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['signTime'];
    }

    /**处理修改时间*/
    if (reqParams?.modifyTime) {
      const [timeStart, timeEnd] = reqParams.modifyTime;
      reqParams['modifyTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['modifyTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['modifyTime'];
    }

    /**处理作废时间*/
    if (reqParams?.cancelTime) {
      const [timeStart, timeEnd] = reqParams.cancelTime;
      reqParams['cancelTimeStart'] = timeStart.format('YYYY-MM-DD 00:00:00');
      reqParams['cancelTimeEnd'] = timeEnd.format('YYYY-MM-DD 23:59:59');
      delete reqParams['cancelTime'];
    }

    delete reqParams['createFormNameDept'];
    delete reqParams['createFormNameUser'];
    if (reqParams?.createrID && reqParams?.createrID?.length > 0) {
      reqParams['createrID'] = reqParams?.createrID?.toString();
    } else {
      delete reqParams['createrID'];
    }

    delete reqParams['cancelFormNameDept'];
    delete reqParams['cancelFormNameUser'];
    if (reqParams?.cancelerID && reqParams?.cancelerID?.length > 0) {
      reqParams['cancelerID'] = reqParams?.cancelerID?.toString();
    } else {
      delete reqParams['cancelerID'];
    }

    run({ ...reqParams });
  }, []);

  /***页面数据及分页信息**/
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /***搜索功能配置columns**/
  const searchList: any = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [searchListParams]);

  return {
    loading,
    reloadRequest,
    areaListData,
    manageTableParams,
    ...tableValues, //页面列表值
    ...searchList,
  };
};

const warnManageService = () => {
  /** 获取列表数据  */
  const { data, loading, refresh } = useRequest(
    (opt: any) => {
      const requestBody: Record<string, any> = {
        current: 1,
        pageSize: 10,
        sort: 'readTime',
        order: 'desc',
        ...opt,
      };

      const requestURL = `${baseUrl}/assetStore/tree`;
      return requestFunComponFiex('GET', requestURL, requestBody);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  return {
    treeLoading: loading,
    treeData: data?.rows,
    reloadRefresh: refresh,
  };
};

export { ManageServices, warnManageService };
